<?php
namespace app\admin\controller;
use app\common\model\DogFamily;
use think\Request;
use think\Db;
use app\common\model\Area as AreaModel;
use app\common\model\Dog as DogModel;
use app\common\model\DogFamily as DogFamilyModel;
class Find extends \app\admin\controller\Base {
    protected $res = array(
        'index'   => array('css' => '2,3,4', 'js' => 'list,My97DatePicker'),
        'notice'   => array('css' => '2,3,4', 'js' => 'echart,My97DatePicker'),
        'reason'   => array('css' => '2,3,4', 'js' => 'echart,My97DatePicker'),
    );
    public function index(){
        $DogModel = new DogModel();
        $AreaModel = new AreaModel();
        $search = input('get.search/a');
        $page = input('get.page');
        $search = $this->search($search, $page);
        $list = $DogModel->findData(15,$search);
        $action = '/' . Request::instance()->controller() . '/' . Request::instance()->action();
        $wherePro['level'] = 1;
        if($search[1]!=''){
            $whereCity['pid'] = $search[1];
        }else{
            $whereCity['pid'] = 2;
        }
        if($search[2]!=''){
            $whereDistrict['pid'] = $search[2];

        }else{
            $whereDistrict['pid'] = 52;

        }
        $province =$AreaModel->selectArea($wherePro);
        $city =$AreaModel->selectArea($whereCity);
        $district =$AreaModel->selectArea($whereDistrict);
        $this->assign(
            array(
                'action'=>$action,
                'list'=>$list,
                'page'=>$page,
                'search' => $search,
                'province' => $province,
                'city' => $city,
                'district' => $district,
                'dog_adopt_status'=>$DogModel::$dog_seek_status
            )
        );
        return $this->fetch();
    }

    /**
     * 查询市
     */
    public function city(){
        $city = $_POST['city'];
        $cityArray = Db::table('up_area')->where('pid',$city)->select();
        $data['cityArray'] = $cityArray;
        return json($data);
    }
    /**
     * 查询区
     */
    public function area(){
        $area = $_POST['area'];
        $cityArray = Db::table('up_area')->where('pid',$area)->select();
        $data['cityArray'] = $cityArray;
        return json($data);
    }

    /**
     * @return mixed|\think\response\Json
     * 详情页面
     */
    public function detail(){
        $id = input('get.id');
        $where['dog_id'] = $id;
        $res = Db::table('up_dog')->where($where)->field('udf_id,dog_type,dog_seek_characteristic')->find();
        if($res['dog_type']!=4){
            return $this->api_dbError("不能查看其他狗的详情");
        }

        $dog = Db::table('up_dog_seek_characteristic')->where('udskc_id',$res['dog_seek_characteristic'])->find();
        $whereC['udskca_id'] = array('IN',$dog['udskca_id']);
        $udskca_name = Db::table('up_dog_seek_characteristic_apparel')->where($whereC)->column('udskca_name');
        if(count($udskca_name)>0){
            if($dog['udskca_id_other']!=''){
                $dog['name'] = implode(',',$udskca_name).','.$dog['udskca_id_other'];
            }else{
                $dog['name'] = implode(',',$udskca_name);
            }
        }else{
            $dog['name'] =$dog['udskca_id_other'];
        }

        $whereE['udsci_id'] = array('IN',$dog['udskci_id']);
        $udsci_name = Db::table('up_dog_seek_characteristic_other')->where($whereE)->column('udsci_name');
        if(count($udsci_name)>0){
            if($dog['udskci_id_other']!=''){
                $dog['udsci_name'] = implode(',',$udsci_name).','.$dog['udskca_id_other'];
            }else{
                $dog['udsci_name'] = implode(',',$udsci_name);
            }
        }else{
            $dog['udsci_name'] =$dog['udskci_id_other'];
        }
        if($dog['udskc_sex']==1){
            $dog['sex']='公';
        }else{
            $dog['sex']='母';
        }

        if($dog['udskc_habitus']==1){
            $dog['habitus']='小型狗';
        }else if($dog['udskc_habitus']==2){
            $dog['habitus']='中型狗';
        }else{
            $dog['habitus']='大型狗';
        }

        if($dog['udskc_hair']==1){
            $dog['hair']='长毛';
        }else{
            $dog['hair']='短毛';
        }

        if($dog['udskc_tail']==1){
            $dog['tail']='长尾';
        }else if($dog['udskc_tail']==2){
            $dog['tail']='短尾';
        }else{
            $dog['tail']='无尾巴';
        }
        $this->assign(array(
            'dog'=>$dog,
        ));
        return $this->fetch();
    }

    /**
     * @return \think\response\Json
     * excel导出
     */
    public function excelDownload(){
        $search = input('post.search/a');
        $DogModel = new DogModel();
        $res = $DogModel->excelData($search,4,'dog_seek_status');
        $result = array();
        foreach ($res as $key=>$vo){
            $result[$key]['id']=$vo['dog_id'];
            if($vo['dog_seek_status']==1){
                $result[$key]['status']='寻找中';
            }elseif ($vo['dog_seek_status']==2){
                $result[$key]['status']='已找回';
            }elseif ($vo['dog_seek_status']==3){
                $result[$key]['status']='暂停寻找';
            }else{
                $result[$key]['status']='';
            }
            $dog_start_time = strtotime($vo['dog_release_time']);
            $result[$key]['release_time'] =date('Y',$dog_start_time).'/'.date('m',$dog_start_time).'/'.date('d',$dog_start_time).' '.date('H',$dog_start_time).':'.date('i',$dog_start_time);

            $dog_seek_time = strtotime($vo['dog_seek_time']);

            $result[$key]['dog_seek_time'] =date('Y',$dog_seek_time).'/'.date('m',$dog_seek_time).'/'.date('d',$dog_seek_time).' '.date('H',$dog_seek_time).':'.date('i',$dog_seek_time);
            $result[$key]['dog_seek_s'] =$vo['province'];
            $result[$key]['dog_seek_city'] =$vo['city'];
            $result[$key]['dog_seek_area'] =$vo['district'];
            $result[$key]['dog_add'] =$vo['dog_add'];

            $result[$key]['dog_seek_so'] =$vo['reason'];
            if($vo['udskc_sex']==1){
                $result[$key]['dog_sex'] ='公';
            }else{
                $result[$key]['dog_sex'] ='母';
            }

            if($vo['udskc_habitus']==1){
                $result[$key]['dog_size'] ='小型狗';
            }else if($vo['udskc_habitus']==2){
                $result[$key]['dog_size'] ='中型狗';
            }else{
                $result[$key]['dog_size'] ='大型狗';
            }

            if($vo['udskc_hair']==1){
                $result[$key]['dog_hair'] ='长毛';
            }else{
                $result[$key]['dog_hair'] ='短毛';
            }

            if($vo['udskc_hair']==1){
                $result[$key]['udf_tail'] ='长尾';
            }else if($vo['udskc_hair']==2){
                $result[$key]['udf_tail'] ='短尾';
            }else{
                $result[$key]['udf_tail'] ='无尾巴';
            }
            $result[$key]['udskca_id_other']  = $vo['name'];

            $result[$key]['udskci_id_other']  = $vo['udsci_name'];
            $result[$key]['dog_information']  = $vo['dog_information']?$vo['dog_information']:'';
            $result[$key]['dog_seek_money_xx']  = $vo['dog_seek_money_xx']?$vo['dog_seek_money_xx']:'';
            $result[$key]['dog_seek_money_ok']  = $vo['dog_seek_money_ok']?$vo['dog_seek_money_ok']:'';
            $result[$key]['user_id']  = $vo['user_id'];


            if($vo['user_attestation']==2||$vo['user_attestation']==3){
                $result[$key]['user_attestation'] ='已认证';
            }else{
                $result[$key]['user_attestation'] ='未认证';
            }
            if($vo['user_gender']==1){
                $result[$key]['user_sex'] ='男';
            }else{
                $result[$key]['user_sex'] ='女';
            }

            if($vo['user_card']!=''){
                $result[$key]['user_age'] = $this->getAgeByID($vo['user_card']);
                $result[$key]['address'] = $this->userCard(substr($vo['user_card'],0,2));

            }else{
                $result[$key]['user_age'] = '';
                $result[$key]['address'] = '';

            }
            $result[$key]['equipment'] = $vo['equipment'];

            $result[$key]['total'] = $vo['total'];
            $result[$key]['dog_id'] = $vo['udf_id'];
        }
        $xlsCell  = array(
            array('id','ID'),
            array('status','状态'),
            array('release_time','发布时间'),
            array('dog_seek_time','失踪时间'),
            array('dog_seek_s','失踪省'),
            array('dog_seek_city','失踪市'),
            array('dog_seek_area','失踪区'),
            array('dog_add','失踪地点'),
            array('dog_seek_so','失踪原因'),
            array('dog_sex','狗狗性别'),
            array('dog_size','狗狗体型'),
            array('dog_hair','狗狗毛发'),
            array('udf_tail','狗狗尾巴'),
            array('udskca_id_other','狗狗穿戴'),
            array('udskci_id_other','狗狗其他特征'),
            array('dog_information','启事内容'),
            array('dog_seek_money_xx','提供信息酬劳'),
            array('dog_seek_money_ok','护送回家酬劳'),
            array('user_id','用户ID'),
            array('user_attestation','是否认证'),
            array('user_sex','用户性别'),
            array('user_age','用户年龄'),
            array('address','户口所在地'),
            array('equipment','设备'),
            array('total','评论数量'),
            array('dog_id','狗狗的ID'),
        );
        $data = $this->exportExcel($xlsCell,$result);
        return $data;
    }

    /**
     * @return mixed
     * 寻狗启事列表
     */
    public function notice(){
        $AreaModel = new AreaModel();
        $search = input('get.search/a');
        $page = input('get.page');
        $search = $this->search($search, $page);
        $action = '/' . Request::instance()->controller() . '/' . Request::instance()->action();
        $wherePro['level'] = 1;
        if($search[4]!=''){
            $whereCity['pid'] = $search[4];
        }else{
            $whereCity['pid'] = 2;
        }
        if($search[5]!=''){
            $whereDistrict['pid'] = $search[5];

        }else{
            $whereDistrict['pid'] = 52;
        }
        $province =$AreaModel->selectArea($wherePro);
        $city =$AreaModel->selectArea($whereCity);
        $district =$AreaModel->selectArea($whereDistrict);
        $this->assign(
            array(
                'action'=>$action,
                'page'=>$page,
                'search' => $search,
                'province' => $province,
                'city' => $city,
                'district' => $district,
            )
        );
        return $this->fetch();
    }

    /***
     * 柱状图统计
     */
    public function echartsColumn(){
        $DogModel = new DogModel();

        $search = input('post.search/a');
        if($search[3]!=''&&$search[4]!=''){
            $date_time = $this->prDates($search[3],$search[4],$search[5]);
        }else{
            $date_time = $this->prDates();
        }

        $count = count($date_time);
        if($search[5]==''){//默认年月日
            $start = $date_time[0];
            $end = $date_time[count($date_time)-1];
            $whereMap['u.dog_time'] = array('between',array($start,$end));
        }else{//证明按了点击按钮
            $whereMap = $this->dateArray($search,$count,$date_time);
        }
        $DogFamilyModel = new DogFamilyModel();
        $dog_total = $DogFamilyModel->countNum(1);//狗窝的总数量

        $total = $this->totalDogStatusNum($search,$whereMap,1);//符合条件的寻狗的数量

        $money_total = $this->totalDogStatusNum(1,1,1);//统计所有的寻狗数量

        $whereM['u.dog_seek_money_xx'] =array('>',0);
        $totalM=$this->totalDogStatusNum($search,$whereMap,$whereM);//提供酬劳的数量

        $where1['u.dog_seek_status'] =1;//寻找中
        $total1=$this->totalDogStatusNum($search,$whereMap,$where1);//寻找中的数量
        $where2['u.dog_seek_status'] =2;//已找回
        $total2=$this->totalDogStatusNum($search,$whereMap,$where2);//已找回的数量
        $where3['u.dog_seek_status'] =3;//暂停寻找
        $total3=$this->totalDogStatusNum($search,$whereMap,$where3);//暂停寻找的数量

        $res['dog_seek_status1'] = $total1;
        $res['dog_seek_status2']= $total2;
        $res['dog_seek_status3'] = $total3;

            if($total!=0){
                $res['dog_seek_status1_num'] = number_format($total1/$total,2)*100;
            }else{
                $res['dog_seek_status1_num'] = $total1*100;
            }


            if($total!=0){
                $res['dog_seek_status2_num'] = number_format($total2/$total,2)*100;
            }else{
                $res['dog_seek_status2_num'] = $total2*100;
            }

            if($total!=0){
                $res['dog_seek_status3_num'] = number_format($total3/$total,2)*100;
            }else{
                $res['dog_seek_status3_num'] = $total3*100;
            }
            if($money_total!=0){
                $res['dog_money'] = number_format($totalM/$money_total,2)*100;
            }else{
                $res['dog_money'] = $totalM*100;
            }

            if($dog_total!=0){
                $res['dog_number'] = number_format($total/$dog_total,2)*100;

            }else{
                $res['dog_number'] = $total*100;

            }

        foreach ($date_time as $key=>$vo){
            $res['time'][]=$vo;
            if($search[5]==1){
                $whereHe['u.dog_time'] = array('elt',$vo);
            }else if($search[5]==2){
                $whereHe['u.dog_month'] = array('elt',$vo);
            }else if($search[5]==3){
                $whereHe['u.dog_year'] = array('elt',$vo);
            }else{
                $whereHe['u.dog_time'] = array('elt',$vo);
            }
            if($search[5]==1){
                $whereHe1['u.dog_time'] = array('EQ',$vo);
            }else if($search[5]==2){
                $whereHe1['u.dog_month'] = array('EQ',$vo);
            }else if($search[5]==3){
                $whereHe1['u.dog_year'] = array('EQ',$vo);
            }else{
                $whereHe1['u.dog_time'] = array('EQ',$vo);
            }
            $value  = $DogModel->findColumnDog($search,$whereHe);
            $value1  = $DogModel->findColumnDog($search,$whereHe1);
            $res['value'][]=$value?$value:0;
            $res['value1'][]=$value1?$value1:0;
        }
        return json(array(
            'totalArray'=>$res
        ));
    }

    /**
     * @param $search
     * @param $whereMap
     * @param string $status
     * @return int|string
     * 统计发布寻狗动态的数量
     */
    public function totalDogStatusNum($search,$whereMap,$whereH=1){
        $where = '';
        if($search[0]!=''){
            $where['u.area_sheng'] = array('=',$search[0]);
        }
        if($search[1]!=''){
            $where['u.area_shi'] = array('=',$search[1]);
        }
        if($search[2]!=''){
            $where['u.area_qu'] = array('=',$search[2]);
        }
        $where['u.dog_type'] = 4;
        $branch = Db::table('up_dog')->alias('u')
            ->where($where)
            ->where($whereMap)
            ->where($whereH)
            ->count();
        //总的数据
        return $branch;
    }
    /***
     * @return mixed
     * 失踪原因
     *
     */
    public function reason(){
        $AreaModel = new AreaModel();
        $search = input('get.search/a');
        $page = input('get.page');
        $search = $this->search($search, $page);
        $action = '/' . Request::instance()->controller() . '/' . Request::instance()->action();
        $wherePro['level'] = 1;
        if($search[4]!=''){
            $whereCity['pid'] = $search[4];
        }else{
            $whereCity['pid'] = 2;
        }
        if($search[5]!=''){
            $whereDistrict['pid'] = $search[5];

        }else{
            $whereDistrict['pid'] = 52;
        }
        $province =$AreaModel->selectArea($wherePro);
        $city =$AreaModel->selectArea($whereCity);
        $district =$AreaModel->selectArea($whereDistrict);
        $this->assign(
            array(
                'action'=>$action,
                'page'=>$page,
                'search' => $search,
                'province' => $province,
                'city' => $city,
                'district' => $district,
            )
        );
        return $this->fetch();
    }

    /**
     * 失踪原因
     */
    public function reasonEcharts(){
        $search = input('post.search/a');
        $DogModel = new DogModel();
        $environment1 = array('reason_id'=>0,'reason_name'=>'其他');
        $environment = Db::table('up_dog_miss_reason')->select();
        array_unshift($environment,$environment1);
        foreach ($environment as $he=>$eo){
//            $reasonWhere = $eo['reason_id'];
            $whereH['u.dog_seek_so'] = array('=',$eo['reason_id']);
            $data = $DogModel->findPie($search,$whereH);
            $missing['value'] =$data;
            $missing['name'] = $eo['reason_name'];
            $result[] = $missing;
        }
        return json($result);
    }

    /***
     * @return \think\response\Json
     * 删除
     */
    public function delDog(){
        $ids = strtoarray(input('post.ids'));
        $DogFamilyModel = new DogModel();
        $where['dog_id'] = array('In', $ids);
        $result = $DogFamilyModel->where($where)->delete();
        if ($result) {
            return $this->api_success('操作成功');
        } else {
            return $this->api_dbError();
        }
    }
}